# -*- coding: utf-8 -*-
from django.shortcuts import render,render_to_response,HttpResponse,redirect
from django.http import HttpResponseRedirect
from django.contrib import auth
from django.contrib.auth import get_user_model
from django.contrib.auth.hashers import make_password
from account.account_api import *
from account.models import *

# Create your views here.

def test_cooking(request,pid=None,del_pass=None):
    if request.session.test_cookie_worked():
        request.session.delete_test_cookie()
        message = "Cookie supported!"
    else:
        message = "Cookie not supported!"

    request.session.set_test_cookie()

    return render(request,'index.html',{'message':message})


def login_user(request):
    if request.session.get('user_name',False):
        return HttpResponseRedirect('/')

    if request.method == 'GET':
        return render(request,'account/login.html')

    if request.method == 'POST':
        username = request.POST.get('username','')
        password = request.POST.get('password','')
        user = auth.authenticate(username=username,password=password)
        if not request.POST.get('remember',None):
            request.session.set_expiry(0)

        if user and user.is_active:
            auth.login(request,user)
            request.session['user_name'] = user.username
            # request.session['user_role'] = user.role
            # request.session['user_id'] = user.id
            return HttpResponseRedirect('/')
        else:
            msg_err = {'error':u'Username or Password ERROR'}
            return render(request,'account/login.html',{'msg_err':msg_err})

@require_login
def logout_user(request):
    auth.logout(request)
    if request.session.get('user_name'):
        del request.session['user_name']
    return HttpResponseRedirect('/')

@require_login
def account_list(request):
    if request.method == 'GET':
        GetUser = User.objects.all()
        kwavr = {
            'GetUser':GetUser,
            'list_role':list_role,
        }
    return render(request,'account/user_list.html',kwavr)

@require_login
def account_add(request):
    if request.method == 'GET':
        Deptlist = Dept.objects.all()
        kwavr = {
            'Deptlist':Deptlist,
            'list_role':list_role,
        }
        return render(request,'account/user_add.html',kwavr)

    if request.method == 'POST':
        if is_common_user(request):
            return HttpResponse(u'普通用户没有权限添加账号')

        username = request.POST.get('username')

        if User.objects.filter(username=username):
            return HttpResponse(u'该用户已存在')
        else:
            nickname = request.POST.get('nickname')
            phone = request.POST.get('phone')
            pass_wd = request.POST.get('passwd')
            pass_wd = make_password(pass_wd)
            email = request.POST.get('email')
            sex = request.POST.get('sex')
            isactive = request.POST.get('isactive')
            dept = request.POST.get('dept')
            dept_name = Dept.objects.get(id=dept)
            role = request.POST.get('role')

            p = User(username=username,nickname=nickname,phone=phone,password=pass_wd,email=email,
                     sex=sex,is_active=isactive,dept=dept_name,role=role)
            p.save()

            return HttpResponse(u'用户添加成功')


@require_login
def account_detail(request):
    Userinfo = request.session.get('user_name')
    Userdata = User.objects.filter(username=Userinfo)
    list_roles = list_role

    return render(request,'account/user_detail.html',locals())

@require_login
def account_edit(request,accid):
    if request.method == 'GET':
        Userinfo = User.objects.filter(id=accid)
        Deptlist = Dept.objects.all()
        edid = accid
        list_roles = list_role

    return render(request,'account/user_edit.html',locals())

    if request.method == 'POST':
            if is_common_user(request):
                return HttpResponse(u"普通用户没有权限!!!")
            Useredit =User.objects.get(id=id)
            Useredit.username = request.POST.get('username')
            Useredit.email = request.POST.get('email')
            Useredit.sex = request.POST.get('sex')
            dept = request.POST.get('dept')
            Useredit.dept = Dept.objects.get(id=dept)
            Useredit.nickname = request.POST.get('nickname')
            Useredit.phone = request.POST.get('phone')
            Useredit.role = request.POST.get('role')
            Useredit.is_active = int(request.POST.get('isactive')) #数据库类型为int所以要进行转换
            Useredit.save()
            return HttpResponse(u"用户修改成功")

@require_login
def account_del(request,accid):
    if request.method == 'GET':
        if is_common_user(request):
            return HttpResponse(u'普通用户没有权限删除')

        if accid == '1':
            return HttpResponse(u'超级管理员不能删除')
        else:
            User.objects.filter(id=accid).delete()
        return HttpResponse(u'用户删除成功')

@require_login
def dept_list(request):

    if request.method == "GET":
        Deptlist = Dept.objects.all()

    return render(request,'account/dept_list.html',{'Deptlist':Deptlist})

@require_login
def dept_del(request,dept_id):
    deldept = Dept.objects.get(id=dept_id)

    if is_common_user(request):
        return HttpResponse(u'普通用户没有权限')
    if deldept == '1':
        return HttpResponse(u'默认部门不能删除')

    if deldept.user_set.all():
        return HttpResponse(u'不能删除的部门，请删除相关用户')
    else:
        Dept.objects.filter(id=dept_id).delete()
        return HttpResponse(u'部门删除成功')

@require_login
def dept_add(request):
    if request.method == 'GET':
        return render(request,'account/dept_add.html')

    if request.method == 'POST':
        dept_name = request.POST.get('dept_name')
        if is_common_user(request):
            return HttpResponse(u'普通用户没有权限')
        if Dept.objects.filter(name=dept_name):
            Warrmess = u'该部门已存在'
            return HttpResponse(Warrmess)
        else:
            p = Dept(name=dept_name)
            p.save()

            return HttpResponse(u'部门添加成功')

@require_login
def dept_edit(request,dept_id):
    if request.method == 'GET':
        GetDept = Dept.objects.filter(id=dept_id)
        return render(request,'account/dept_edit.html',{'GetDept':GetDept})

    if request.method == 'POST':
        if is_common_user(request):
            Warrmess = u'普通用户没有权限修改'
            return HttpResponse(Warrmess)

        DepDeptname = request.POST.get('dept_name')

        if Dept.objects.filter(id=dept_id,name=DepDeptname):
            GetDept = Dept.objects.get(id=dept_id)
            GetDept.name = request.POST.get('dept_name')
            GetDept.save()
            return HttpResponse(u'部门修改成功')

        if Dept.objects.filter(name=DepDeptname):
            return HttpResponse(u'该部门已存在')
        else:
            GetDept = Dept.objects.get(id=dept_id)
            GetDept.name = DepDeptname
            GetDept.save()
            return HttpResponse(u'部门修改成功')





